package com.wlts.wzy.sys_shiro.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.sys_shiro.entity.*;
import com.wlts.wzy.sys_shiro.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户角色关系表 前端控制器
 * </p>
 *
 * @author wuzhiyong
 * @since 2019-04-22
 */
@ApiIgnore
@RestController
@RequestMapping("/sys-user-role")
public class SysUserRoleController {
    @Autowired
    private SysUserRoleServiceImpl sysUserRoleService;

    @Autowired
    private SysUserServiceImpl sysUserService;
    @Autowired
    private SysRoleServiceImpl roleService;
    @Autowired
    private SysRolePermissionServiceImpl rolePermissionService;
    @Autowired
    private SysPermissionServiceImpl permissionService;
    /**
     * 增加用户角色关系
     * @param sysUserRole
     * @return
     */
    @RequestMapping("buildSysUserRole")
    public Result buildSysUserRole(@RequestBody SysUserRole sysUserRole){
       // System.out.println(sysUserRole.toString());
        SysUser sysUser=sysUserService.getById(sysUserRole.getUserId());
        if (sysUser==null){
            return new Result<>(500,"用户为空!");
        }
        SysRole sysRole=roleService.getById(sysUserRole.getRoleId());
        if (sysRole==null){
            return new Result<>(500,"角色用户为空!");
        }

        SysUserRole sysUserRole1=sysUserRoleService.getOne(new QueryWrapper<SysUserRole>().eq("user_id",sysUserRole.getUserId()).eq("role_id",sysUserRole.getRoleId()));
        if(sysUserRole1==null){
            sysUserRoleService.save(sysUserRole);
              return new Result<>();
        }
        return new Result<>(500,"用户角色关系重复！");
    }
    /**
     * 增加用户角色关系LIST
     * @param sysUserRoleList
     * @return
     */
    @RequestMapping("buildSysUserRoleList")
    public Result buildSysUserRoleList(@RequestBody List<SysUserRole> sysUserRoleList){
        for (SysUserRole sysUserRole:sysUserRoleList
        ) {
            SysUser sysUser=sysUserService.getById(sysUserRole.getUserId());
            if (sysUser!=null){
                SysRole sysRole=roleService.getById(sysUserRole.getRoleId());
                if (sysRole!=null){
                    SysUserRole sysUserRole1=sysUserRoleService.getOne(new QueryWrapper<SysUserRole>().eq("user_id",sysUserRole.getUserId()).eq("role_id",sysUserRole.getRoleId()));
                    if(sysUserRole1==null){
                        sysUserRoleService.save(sysUserRole);
                    }
                }
            }
        }
          return new Result<>();
    }

    //删除
    @RequestMapping("deleteSysUserRole")
    public Result deleteSysUserRole(@RequestBody SysUserRole sysUserRole) {
        SysUserRole sysUserRole1=sysUserRoleService.getOne(new QueryWrapper<SysUserRole>().eq("user_id",sysUserRole.getUserId()).eq("role_id",sysUserRole.getRoleId()));
        if (sysUserRole1 != null) {
            sysUserRoleService.removeById(sysUserRole1);
              return new Result<>();
        }
        return new Result<>(500,"删除失败！");
    }

    /**
     * 查看用户拥有的角色和权限
     */
    @RequestMapping("seeSysUserRole")
    public Result seeSysUserRole(@RequestBody SysUserRole sysUserRole){
        List<SysUserRole> secUserRoles = sysUserRoleService.list(new QueryWrapper<SysUserRole>().eq("user_id", sysUserRole.getUserId()));
        //查询所有权限
        List<SysRole> sysRoles = new ArrayList<SysRole>();
        //查询所有权限
        List<SysPermission> permissions = new ArrayList<SysPermission>();
        for (SysUserRole userRole:secUserRoles) {
            SysRole role =roleService.getById(userRole.getRoleId());
            if (role!=null){
                sysRoles.add(role);
                List<SysRolePermission> rolePermissions = rolePermissionService.list(new QueryWrapper<SysRolePermission>().eq("role_id", role.getId()));
                for (SysRolePermission rolePermission:rolePermissions) {
                    SysPermission permission = permissionService.getById(rolePermission.getPermissionId());
                    permissions.add(permission);
                }
            }
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("sysRoles",sysRoles);
        jsonObject.put("permissions",permissions);
          return Result.buildResult(jsonObject);
    }

    //查看角色下的用户
    @RequestMapping("SeeRoleUser")
    public Result SeeRoleUser(@RequestBody SysUserRole sysUserRole){
        List<SysUserRole> sysUserRoleList=sysUserRoleService.list(new QueryWrapper<SysUserRole>().eq("role_id",sysUserRole.getRoleId()));
        List<SysUser> sysUserList = new ArrayList<SysUser>();
        for (SysUserRole sysUserRole1:sysUserRoleList) {
            SysUser sysUser = sysUserService.getById(sysUserRole1.getUserId());
            sysUserList.add(sysUser);
        }
          return Result.buildResult(sysUserList);
    }
}
